#include "ThreadPool.h"

//#include "threadpool.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

const int MAX_TASKS = 4;

typedef struct XDownloadStartReplyMsg {
	int type;
	int size;
    unsigned char *buf;
} XDownloadStartReplyMsg;

void hello(void* arg)
{
  XDownloadStartReplyMsg* x = (XDownloadStartReplyMsg*) arg;
  printf("msg type %x, size %d, buf [%s]!", x->type, x->size, x->buf);

  //wait data 

  //do something
  
  //quit
}

int main(int argc, char* argv[])
{
  ThreadPool tp(2);
  int ret = tp.initialize_threadpool();
  if (ret == -1) {
    cerr << "Failed to initialize thread pool!" << endl;
    return 0;
  }

  for (int i = 0; i < MAX_TASKS; i++) {
    XDownloadStartReplyMsg* x = new XDownloadStartReplyMsg;
    x->type=i;
    x->size=i*512;
    x->buf=new unsigned char[x->size];
    sprintf((char *)x->buf, " test %d sample\n", i);
    Task* t = new Task(&hello, (void*) x);
//    cout << "Adding to pool, task " << i+1 << endl;
    tp.add_task(t);
//    cout << "Added to pool, task " << i+1 << endl;
  }

  sleep(2);

  tp.destroy_threadpool();

  // TODO: delete worker objects

  cout << "Exiting app..." << endl;

  return 0;
}
